package org.tacademy.basic.calendar;

import java.util.StringTokenizer;

import org.tacademy.basic.calendar.databasemanager.DBConstant;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class InfoSchedule extends DialogFragment {

	ItemData data = new ItemData();
	Cursor mCursor;
	
	private  TextView title;
	private  TextView time;
	private  TextView date;
	private  TextView content;
	private  TextView location;
	
	double latitude, longtitude;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setStyle(STYLE_NO_TITLE, 0);
	}
	
	public InfoSchedule(Cursor cursor){
		mCursor = cursor;
		//if(cursor==null)
		//	Log.i("cal", "null");
	}
	
	public InfoSchedule(ItemData object) {
		 //TODO Auto-generated constructor stub
		data = object;
	}
	
	public ItemData getItemData(){
		int titleIndex = mCursor.getColumnIndex(DBConstant.ScheduleTable.TITLE);
		int dateIndex = mCursor.getColumnIndex(DBConstant.ScheduleTable.DATE);
		int timeIndex = mCursor.getColumnIndex(DBConstant.ScheduleTable.TIME);
		int contentIndex = mCursor.getColumnIndex(DBConstant.ScheduleTable.CONTENT);
		int locationIndex = mCursor.getColumnIndex(DBConstant.ScheduleTable.LOCATION);
		
		ItemData data = new ItemData();
		data.title = mCursor.getString(titleIndex);
		data.time = mCursor.getString(timeIndex);
		data.content = mCursor.getString(contentIndex);
		data.location = mCursor.getString(locationIndex);
		String str = mCursor.getString(dateIndex);
		
		StringTokenizer st = new StringTokenizer(str,"-");
		String temp = "";
		int count=0;
		while(st.hasMoreElements()){
			temp = st.nextToken();
			if(count==0){
				data.year = Integer.parseInt(temp);
				count++;
			}else if(count==1){
				data.month = Integer.parseInt(temp);
				count++;
			}else if(count==2){
				data.day = Integer.parseInt(temp);
				count++;
			}	
		}
		
		return data;
	}
	
	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		AlertDialog.Builder mBuilder = new AlertDialog.Builder(getActivity());
		LayoutInflater inflater = getActivity().getLayoutInflater();
		
		View view = getActivity().getLayoutInflater().inflate(R.layout.info_schedule, null);
		mBuilder.setView(view);
		mBuilder.setTitle("Schedule Detail Information");
		
		data = getItemData();
		
		title = (TextView)view.findViewById(R.id.info_title);
		time = (TextView)view.findViewById(R.id.info_time);
		content = (TextView)view.findViewById(R.id.info_content);
		date = (TextView)view.findViewById(R.id.info_date);
		location = (TextView)view.findViewById(R.id.info_ocation);
		
		String str = data.location;
		StringTokenizer st = new StringTokenizer(str, ",");
		String temp = "";
		int count=0;
		while(st.hasMoreElements()){
			temp = st.nextToken();
			if(count==0){
				data.location = temp;
				count++;
			}else if(count==1){
				latitude = Double.parseDouble(temp);
				count++;
			}else if(count==2){
				longtitude = Double.parseDouble(temp);
				count++;
			}	
		}
		
		title.setText(data.title);
		time.setText(data.time);
		content.setText(data.content);
		date.setText(data.year+"-"+data.month+"-"+data.day);
		location.setText(data.location);
		
		Button btn = (Button)view.findViewById(R.id.showmapdata);
		btn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(getActivity(), ShowLocation.class);
				intent.putExtra("my_latitude", latitude);
				intent.putExtra("my_longtitude", longtitude);
				startActivity(intent);
			}
		});
		
		mBuilder.setPositiveButton("Ok", new OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				dismiss();
			}
		});
		
		return mBuilder.create();
	}
	
	@Override
	public void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
	}
}

